/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Alejo
 */
public class BinaryTree<E> {
protected E val; // value associated with node
protected BinaryTree<E> parent; // parent of node
protected BinaryTree<E> left, right; // children of node
private int count =0;
public BinaryTree()
// post: constructor that generates an empty node
{
val = null;
parent = null; 
left = right = this;
}
public BinaryTree(E value)
// post: returns a tree referencing value and two empty subtrees
{
val = value;
right = left = new BinaryTree<E>();
setLeft(left);
setRight(right);
count ++;
}
public BinaryTree(E value, BinaryTree<E> left, BinaryTree<E> right)
// post: returns a tree referencing value and two subtrees
{
val = value;
if (left == null) { left = new BinaryTree<E>(); }
setLeft(left);
if (right == null) { right = new BinaryTree<E>(); }
setRight(right);
count ++;
}
public void setLeft(BinaryTree<E> newLeft)
// post: sets left subtree to newLeft
// re-parents newLeft if not null
{
if (isEmpty()) return;
if (left != null && left.parent() == this) left.setParent(null);
left = newLeft;
left.setParent(this);
}
public void setRight(BinaryTree<E> newLeft)
// post: sets left subtree to newLeft
// re-parents newLeft if not null
{
if (isEmpty()) return;
if (right != null && right.parent() == this) right.setParent(null);
right = newLeft;
right.setParent(this);
}


protected void setParent(BinaryTree<E> newParent)
// post: re-parents this node to parent reference, or null
        {
if (!isEmpty()) {
parent = newParent;
}
}
 public BinaryTree<E> left()
// post: returns reference to (possibly empty) left subtree
{
return left;
}
 public BinaryTree<E> right()
// post: returns reference to (possibly empty) left subtree
{
return right;
}
 public BinaryTree<E> parent()
// post: returns reference to (possibly empty) left subtree
{
return parent;
}
 public E value()
// post: returns value associated with this node
{
return val;
}
public void setValue(E value)
// post: sets the value associated with this node
{
val = value;
}
public boolean isEmpty(){
boolean vacio = false;
if (count == 0 )
vacio = true;
return vacio;
}
}

